package cn.zy.order.service.impl;

import cn.zy.basic.util.LoginContext;
import cn.zy.basic.util.PageList;
import cn.zy.order.domain.OrderPetAcquisition;
import cn.zy.order.dto.OrderDto;
import cn.zy.order.mapper.OrderPetAcquisitionMapper;
import cn.zy.order.query.OrderPetAcquisitionQuery;
import cn.zy.order.service.IOrderPetAcquisitionService;
import cn.zy.basic.service.impl.BaseServiceImpl;
import cn.zy.org.domain.Employee;
import cn.zy.org.domain.Shop;
import cn.zy.org.mapper.EmployeeMapper;
import cn.zy.org.mapper.ShopMapper;
import cn.zy.pet.domain.Pet;
import cn.zy.pet.domain.SearchMasterMsg;
import cn.zy.pet.mapper.PetMapper;
import cn.zy.pet.mapper.SearchMasterMsgMapper;
import cn.zy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id()!=null){//店铺管理员 or 店员
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdminId()!=employee.getId()){//店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void cancel(Long id) {
        //1.shopId置空,寻主消息state改为3寻主池
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);

    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        // orderId ，支付方式，成交价
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer payType = orderDto.getPayType();
        //1.生成宠物
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        Pet pet = createPet(msg,money);
        petMapper.save(pet);
        //2.修改订单状态1-已完成,并且要修改金额,最终成交价,设置petId
        order.setPetId(pet.getId());
        order.setState(1);
        order.setPrice(money);
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主消息的状态:state=2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        //pet.setUserId(msg.getUserId());//领养人id不在这里设置
        pet.setSearchMasterMsgId(msg.getId());

        return pet;
    }
}
